#include "dlgreporterror.h"
#include "ui_dlgreporterror.h"

#include <QDateTime>
#include "../mainwindow.h"

DlgReportError::DlgReportError(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DlgReportError)
{
    ui->setupUi(this);
    ui->tableWidget_BK->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    ui->tableWidget_BK->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableWidget_BK->setColumnWidth(0,80);
    ui->tableWidget_BK->setColumnWidth(1,60);
    //ui->tableWidget_BK->setColumnWidth(2,200);
    ui->tableWidget_BK->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch);

}

DlgReportError::~DlgReportError()
{
    delete ui;
}

void DlgReportError::slot_Executive(QString Type, QString pid,QString data)
{
    qDebug()<<"DlgReportError::slot_Executive="<<Type<<pid<<data;
    if(Type == "bkDriveStatu")
    {
        QStringList numList= data.split(",");
        for(int i=0;i<numList.length();i++)
        {
            if(QString(numList[i]).length() <= 0)
                continue;
            bool isHave = false;
            for(int row=0;row<ui->tableWidget_BK->rowCount();row++)
                if(ui->tableWidget_BK->item(row,1)->text() == QString("%1-%2").arg(pid).arg(numList[i]))
                    isHave = true;
            if(!isHave)
            {
                int row = ui->tableWidget_BK->rowCount();
                ui->tableWidget_BK->insertRow(row);
                ui->tableWidget_BK->setItem(row,2,new QTableWidgetItem());
                switch (QString(numList[i]).toInt())
                {
                case 0:ui->tableWidget_BK->item(row,2)->setText(QString(u8"编码器ABZ/未连接"));break;
                case 1:ui->tableWidget_BK->item(row,2)->setText(QString(u8"编码器ABZ/未连接"));break;
                case 2:ui->tableWidget_BK->item(row,2)->setText(QString(u8"编码器UVW/编码器内部"));break;
                case 3:ui->tableWidget_BK->item(row,2)->setText(QString(u8"编码器计数/编码器CRC"));break;
                case 4:ui->tableWidget_BK->item(row,2)->setText(QString(u8"驱动器温度"));break;
                case 5:ui->tableWidget_BK->item(row,2)->setText(QString(u8"过压"));break;
                case 6:ui->tableWidget_BK->item(row,2)->setText(QString(u8"低压"));break;
                case 7:ui->tableWidget_BK->item(row,2)->setText(QString(u8"过流"));break;
                case 8:ui->tableWidget_BK->item(row,2)->setText(QString(u8"吸收电阻"));break;
                case 9:ui->tableWidget_BK->item(row,2)->setText(QString(u8"位置跟随误差"));break;
                case 10:ui->tableWidget_BK->item(row,2)->setText(QString(u8"逻辑低压"));break;
                case 11:ui->tableWidget_BK->item(row,2)->setText(QString(u8"电机或驱动器iit"));break;
                case 12:ui->tableWidget_BK->item(row,2)->setText(QString(u8"脉冲频率过高"));break;
                case 13:ui->tableWidget_BK->item(row,2)->setText(QString(u8"电机温度"));break;
                case 14:ui->tableWidget_BK->item(row,2)->setText(QString(u8"电机励磁报警/编码器通讯"));break;
                case 15:ui->tableWidget_BK->item(row,2)->setText(QString(u8"EEPROM"));break;
                case 16:ui->tableWidget_BK->item(row,2)->setText(QString(u8"电流传感器"));break;
                case 17:ui->tableWidget_BK->item(row,2)->setText(QString(u8"看门狗"));break;
                case 18:ui->tableWidget_BK->item(row,2)->setText(QString(u8"异常中断"));break;
                case 19:ui->tableWidget_BK->item(row,2)->setText(QString(u8"MCU ID"));break;
                case 20:ui->tableWidget_BK->item(row,2)->setText(QString(u8"电机配置"));break;
                case 21:ui->tableWidget_BK->item(row,2)->setText(QString(u8"逻辑输出"));break;
                case 22:ui->tableWidget_BK->item(row,2)->setText(QString(u8"STO1"));break;
                case 23:ui->tableWidget_BK->item(row,2)->setText(QString(u8"STO2"));break;
                case 24:ui->tableWidget_BK->item(row,2)->setText(QString(u8"外部使能"));break;
                case 25:ui->tableWidget_BK->item(row,2)->setText(QString(u8"正限位"));break;
                case 26:ui->tableWidget_BK->item(row,2)->setText(QString(u8"负限位"));break;
                case 27:ui->tableWidget_BK->item(row,2)->setText(QString(u8"SPI内部"));break;
                case 28:ui->tableWidget_BK->item(row,2)->setText(QString(u8"CAN总线中断"));break;
                case 29:ui->tableWidget_BK->item(row,2)->setText(QString(u8"全闭环方向"));break;
                case 30:ui->tableWidget_BK->item(row,2)->setText(QString(u8"主编码器ABZ"));break;
                case 31:ui->tableWidget_BK->item(row,2)->setText(QString(u8"主编码器计数"));break;
                case 32:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Ready_on 就绪"));break;
                    // case 33:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Switched_on 驱动器使能"));break;
                    // case 34:ui->tableWidget_BK->item(row,2)->setText(QString(u8"工作模式使能"));break;
                    // case 35:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Fault 报警"));break;
                    // case 36:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Voltage_enable 动力电输入"));break;
                    // case 37:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Quick_stop 快速停止"));break;
                    // case 38:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Switchon_disabled"));break;
                    // case 39:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Warning 警告"));break;
                    // case 40:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Maunufacture0"));break;
                    // case 41:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Remote 远程控制"));break;
                    // case 42:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Target_reached 目标到达"));break;
                    // case 43:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Intlim_active 正/负向限位"));break;
                    // case 44:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Setpoint_Ack"));break;
                    // case 45:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Fllowing_Error"));break;
                    // case 46:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Commutation_Found"));break;
                    // case 47:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Reference_Found"));break;
                 case 48:ui->tableWidget_BK->item(row,2)->setText(QString(u8"Drive Socket Error"));break;
                default:
                    break;
                }

                if(ui->tableWidget_BK->item(row,2)->text().length() > 0)
                {
                    QString stetime = QDateTime::currentDateTime().toString("hh:mm:ss");
                    ui->tableWidget_BK->setItem(row,0,new QTableWidgetItem(stetime));
                    ui->tableWidget_BK->setItem(row,1,new QTableWidgetItem(QString("%1-%2").arg(pid).arg(numList[i])));
                }
                else
                    ui->tableWidget_BK->removeRow(row);
            }
        }
    }

    else
    {
        bool isHave = false;
        int row = 0;
        for(row=0;row<ui->tableWidget_BK->rowCount();row++)
            if(ui->tableWidget_BK->item(row,1)->text() == Type)
            {
                QString stetime = QDateTime::currentDateTime().toString("hh:mm:ss");
                ui->tableWidget_BK->item(row,0)->setText(stetime);
                ui->tableWidget_BK->item(row,2)->setText(data);
                isHave = true;
            }
        qDebug()<<"!isHave="<<!isHave;
        if(!isHave)
        {
            int row = ui->tableWidget_BK->rowCount();
            ui->tableWidget_BK->insertRow(row);
            QString stetime = QDateTime::currentDateTime().toString("hh:mm:ss");
            ui->tableWidget_BK->setItem(row,0,new QTableWidgetItem(stetime));
            ui->tableWidget_BK->setItem(row,1,new QTableWidgetItem(Type));
            ui->tableWidget_BK->setItem(row,2,new QTableWidgetItem(data));
        }
    }
}

void DlgReportError::setParent(void *parent)
{
    m_parent = parent;
}

void DlgReportError::on_btn_bkInit_clicked()
{
    MainWindow *parent = (MainWindow*)m_parent;
    parent->sig_NaviSendData("ControlBkWarn");
    for(int i=ui->tableWidget_BK->rowCount()-1;i>=0;i--)
        ui->tableWidget_BK->removeRow(i);
}

